/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TerrainControl.h"

#include <QtGui/QLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QToolBar>
#include <QtGui/QToolButton>
#include <QtGui/QGroupBox>

#include "NumericTextEdit.h"
#include "TerrainTextureDialog.h"

namespace PQBuilder
{
	TerrainGlobalSettingControl::TerrainGlobalSettingControl(QWidget *parent,PQEngine::TerrainGlobalSetting setting)
		:QWidget(parent)
	{
		QGridLayout* grid=new QGridLayout(this);
		
		this->setLayout(grid);
	}

	TerrainGlobalSettingControl::~TerrainGlobalSettingControl()
	{

	}

	TerrainLayerPaintControl::TerrainLayerPaintControl(QWidget *parent,PQEngine::TerrainLayerPaintSetting setting)
		:QWidget(parent)
	{
		float size=setting.BrushSize;
		float opacity=setting.Opacity;
		
		QToolBar* toolbar=new QToolBar(this);
		QToolButton* addLayerButton=new QToolButton(toolbar);
		addLayerButton->setText("Add Layer");
		toolbar->addWidget(addLayerButton);
		_layerList=new TerrainLayerControl(this);
		_layerList->setMaximumHeight(80);

		_paintControl=new TerrainBrushControl(this);

		QLabel* labelSize=new QLabel(QObject::tr("Size"),this);
		QLabel* labelOpacity=new QLabel(QObject::tr("Opacity"),this);
		NumericTextEdit* editSize=new NumericTextEdit(this);
		editSize->setFloat(size);

		NumericTextEdit* editOpacity=new NumericTextEdit();
		editOpacity->setFloat(opacity);

		QGroupBox* settingGroup=new QGroupBox(QObject::tr("Brush Setting"),this);
		QGridLayout* grid=new QGridLayout(this);
		grid->addWidget(labelSize,0,0);
		grid->addWidget(editSize,0,1);
		grid->addWidget(labelOpacity,1,0);
		grid->addWidget(editOpacity,1,1);

		settingGroup->setLayout(grid);
		settingGroup->setMaximumHeight(32*3);

		QGroupBox* brushGroup=new QGroupBox(QObject::tr("Brush Type"),this);
		grid=new QGridLayout(this);
		grid->addWidget(_paintControl,0,0);
		brushGroup->setLayout(grid);

		grid=new QGridLayout(this);
		grid->addWidget(toolbar,0,0);
		grid->addWidget(_layerList,1,0);
		grid->addWidget(settingGroup,2,0);
		grid->addWidget(brushGroup,3,0);
		this->setLayout(grid);

		connect(addLayerButton,SIGNAL(clicked()),this,SLOT(OnAddLayer()));
	}

	void TerrainLayerPaintControl::OnAddLayer()
	{
		QString path=TerrainTextureDialog::getTexturePath();
		if(!path.isNull()){
			_layerList->addTerrainTextureLayer(path);
		}
	}

	TerrainLayerPaintControl::~TerrainLayerPaintControl()
	{

	}

	TerrainHeightPaintControl::TerrainHeightPaintControl(QWidget *parent,PQEngine::TerrainHeightPaintSetting setting)
		:QWidget(parent)
	{
		float size=setting.BrushSize;
		float opacity=setting.Opacity;
		
		_paintControl=new TerrainBrushControl(this);

		QLabel* labelSize=new QLabel(QObject::tr("Size"),this);
		QLabel* labelOpacity=new QLabel(QObject::tr("Opacity"),this);
		NumericTextEdit* editSize=new NumericTextEdit(this);
		editSize->setFloat(size);

		NumericTextEdit* editOpacity=new NumericTextEdit();
		editOpacity->setFloat(opacity);

		QGroupBox* settingGroup=new QGroupBox(QObject::tr("Brush Setting"),this);
		QGridLayout* grid=new QGridLayout(this);
		grid->addWidget(labelSize,0,0);
		grid->addWidget(editSize,0,1);
		grid->addWidget(labelOpacity,1,0);
		grid->addWidget(editOpacity,1,1);

		settingGroup->setLayout(grid);
		settingGroup->setMaximumHeight(32*3);

		QGroupBox* brushGroup=new QGroupBox(QObject::tr("Brush Type"),this);
		grid=new QGridLayout(this);
		grid->addWidget(_paintControl,0,0);
		brushGroup->setLayout(grid);

		grid=new QGridLayout(this);
		grid->addWidget(settingGroup,0,0);
		grid->addWidget(brushGroup,1,0);
		this->setLayout(grid);
	}

	TerrainHeightPaintControl::~TerrainHeightPaintControl()
	{
	}

	TerrainControl::TerrainControl(QWidget *parent,PQEngine::TerrainComponent* component)
		:QWidget(parent)
	{
		if(component!=0){
			QGridLayout* grid=new QGridLayout(this);
			QTabWidget* tabWidget=new QTabWidget(this);

			_heightPaintControl=new TerrainHeightPaintControl(0,component->getHeightPaintSetting());
			_layerPaintControl=new TerrainLayerPaintControl(0,component->getLayerPaintSetting());
			_globalSettingControl=new TerrainGlobalSettingControl(0,component->getGlobalSetting());
			
			tabWidget->addTab(_heightPaintControl,QObject::tr("Height"));
			tabWidget->addTab(_layerPaintControl,QObject::tr("Layer"));
			tabWidget->addTab(_globalSettingControl,QObject::tr("Setting"));

			grid->addWidget(tabWidget,0,0);
			this->setLayout(grid);
			

			
			//SEngine.setTerrainEditMode(PQEngine::TERRAIN_EDIT_HIGHT);
			
			connect(tabWidget,SIGNAL(currentChanged(int)),this,SLOT(OnTabChanaged(int)));
		}
	}

	TerrainControl::~TerrainControl()
	{

	}

	void TerrainControl::OnTabChanaged(int index)
	{
		assert(false);
		/*
		switch(index){
			case 0:
				SEngine.setTerrainEditMode(PQEngine::TERRAIN_EDIT_HIGHT);
				break;
			case 1:
				SEngine.setTerrainEditMode(PQEngine::TERRAIN_EDIT_LAYER);
				break;
			case 2:
				SEngine.setTerrainEditMode(PQEngine::TERRAIN_EDIT_SETTING);
				break;
			default:
				SEngine.setTerrainEditMode(PQEngine::TERRAIN_EDIT_NONE);
				break;
		}
		*/
	}
}
